<?php

require_once("config.php");
require_once("utils.php");
require_once("Component.php");

class Contact extends Component {

	var $defaultColumns;
	var $columnMappings;
	
    function __construct($proxy) {

		$this->defaultColumns = array(
                                        'salutation',
                                        'first_name',
                                        'last_name',
                                        'account_id',
                                        'lead_source',
                                        'campaign_id',
                                        'title',
                                        'department',
                                        'assigned_user_id',
                                        'email1',
                                        'phone_work',
                                        'phone_mobile',
                                        'phone_home',
                                        'phone_other',
                                        'phone_fax',
                                        'do_not_call',
                                        'primary_address_street',
                                        'primary_address_city',
                                        'primary_address_state',
                                        'primary_address_postalcode',
                                        'primary_address_country',
                                        'description',
                                        );
		
		$this->columnMappings = array("contacts.account_id" => "accounts_contacts.account_id");

		parent::__construct("Contacts", "contacts", $proxy, $this->defaultColumns);
	}


	function saveRecord($id, $photo_c, $salutation, $first_name, $last_name,
 						     $account_id, $lead_source, $campaign_id,
							 $title, $department, $assigned_user_id,
							 $email1, $phone_work, $phone_mobile, $phone_home, $phone_other,
							 $phone_fax, $do_not_call, $primary_address_street,
							 $primary_address_city, $primary_address_state,
							 $primary_address_postalcode, 
							 $primary_address_country, $description,
							 $date_modified) {
		
			if (!$assigned_user_id)
				$assigned_user_id = $_SESSION["logged_in_user_id"];
				
			if ($id) {
				$call_arguments = array('session' => $this->sess, 
	    				'module' => 'Contacts', 
	    				'name_value_list' =>array(
							array("name" => 'id',"value" => $id),
					  		array("name" => 'salutation',"value" => $salutation),
				      		array("name" => 'first_name',"value" => $first_name),
		                  	array("name" => 'last_name',"value" => $last_name),
							  array("name" => 'account_id',"value" => $account_id),
							  array("name" => 'lead_source',"value" => $lead_source),
							  array("name" => 'campaign_id',"value" => $campaign_id),
							  array("name" => 'title',"value" => $title),
							  array("name" => 'department',"value" => $department),
							  array("name" => 'assigned_user_id',"value" => $assigned_user_id),
							  array("name" => 'email1',"value" => $email1),
							  array("name" => 'phone_work',"value" => $phone_work),
							  array("name" => 'phone_mobile',"value" => $phone_mobile),
							  array("name" => 'phone_home',"value" => $phone_home),
							  array("name" => 'phone_other',"value" => $phone_other),
							  array("name" => 'phone_fax',"value" => $phone_fax),
							  array("name" => 'do_not_call',"value" => $do_not_call),
							  array("name" => 'primary_address_street',"value" => $primary_address_street),
							  array("name" => 'primary_address_city',"value" => $primary_address_city),
							  array("name" => 'primary_address_state',"value" => $primary_address_state),
							  array("name" => 'primary_address_postalcode',"value" => $primary_address_postalcode),
							  array("name" => 'primary_address_country',"value" => $primary_address_country),
							  array("name" => 'description',"value" => $description),
							  array("name" => 'date_modified',"value" => $date_modified)
	            		)
	            );
	            $response = $this->rest_request('set_entry', $call_arguments);
			} else {
            			
				$call_arguments = array(
					'session' => $this->sess, 
    				'module' => 'Contacts', 
    				'name_value_list' =>array(
						array("name" => 'salutation',"value" => $salutation),
					      array("name" => 'first_name',"value" => $first_name),
	            	      array("name" => 'last_name',"value" => $last_name),
						  array("name" => 'account_id',"value" => $account_id),
						  array("name" => 'lead_source',"value" => $lead_source),
						  array("name" => 'campaign_id',"value" => $campaign_id),
						  array("name" => 'title',"value" => $title),
						  array("name" => 'department',"value" => $department),
						  array("name" => 'assigned_user_id',"value" => $assigned_user_id),
						  array("name" => 'email1',"value" => $email1),
						  array("name" => 'phone_work',"value" => $phone_work),
						  array("name" => 'phone_mobile',"value" => $phone_mobile),
						  array("name" => 'phone_home',"value" => $phone_home),
						  array("name" => 'phone_other',"value" => $phone_other),
						  array("name" => 'phone_fax',"value" => $phone_fax),
						  array("name" => 'do_not_call',"value" => $do_not_call),
						  array("name" => 'primary_address_street',"value" => $primary_address_street),
						  array("name" => 'primary_address_city',"value" => $primary_address_city),
						  array("name" => 'primary_address_state',"value" => $primary_address_state),
						  array("name" => 'primary_address_postalcode',"value" => $primary_address_postalcode),
						  array("name" => 'primary_address_country',"value" => $primary_address_country),
	            		  array("name" => 'description',"value" => $description),
	            	      array("name" => 'date_modified',"value" => $date_modified)
					)
				);
				$response = $this->rest_request('set_entry', $call_arguments);
            }
			## If new account is associated, set the prvious account to deleted in accounts_contacts
			return $response;
	}
		
}
	
$component = new Contact($sugarcrm_rest_url);

$salutationsArray = array("" => "", 
 						  "Mr." => "Mr.", 
						  "Ms." => "Ms.", 
						  "Mrs." => "Mrs.", 
						  "Dr." => "Dr.", 
						  "Prof." => "Prof.");
						  
$leadsArray = array("" => "",
				    "Cold Call" => "Cold Call",
					"Existing Customer" => "Existing Customer",
					"Self Generated" => "Self Generated",
					"Employee" => "Employee",
					"Partner" => "Partner",
					"Public Relations" => "Public Relations",
					"Direct Mail" => "Direct Mail",
					"Conference" => "Conference",
					"Trade Show" => "Trade Show",
					"Web Site" => "Web Site",
					"Word of mouth" => "Word of mouth",
					"Email" => "Email",
					"Campaign" => "Campaign",
					"Other" => "Other");
?>
